package com.itelite.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itelite.common.info.Result;
import com.itelite.domain.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 **/
@Component
@Slf4j
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Autowired
    private ObjectMapper objectMapper;
    //注销成功
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("用户"+((User)authentication.getPrincipal()).getUsername()+"注销");
        response.getWriter().write(objectMapper.writeValueAsString(Result.success("注销成功")));
    }
}
